package com.hbnu.sync.domain.base;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 通用返回对象
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Rest<T> implements Serializable {
    @ApiModelProperty(value = "响应码")
    private int code;
    @ApiModelProperty("提示信息")
    private String msg;
    @ApiModelProperty("数据")
    private T data;
    @ApiModelProperty("时间戳")
    private long timestamp;
    @ApiModelProperty("数据个数")
    private int count;
    protected Rest() {
    }

    /**
     * Ok rest.
     *
     * @return the rest
     */
    public static <T> Rest<T> success() {
        Rest<T> result = new Rest<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMessage());
        result.setTimestamp();
        return result;
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> Rest<T> success(T data) {
        Rest<T> result = new Rest<>();
        result.setData(data);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMessage());
        result.setTimestamp();
        return result;
    }

    /**
     * 成功返回结果
     *
     * @param data    获取的数据
     * @param message 提示信息
     */
    public static <T> Rest<T> success(T data, String message) {
        Rest<T> result = new Rest<>();
        result.setData(data);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(message);
        result.setTimestamp();
        return result;
    }
    public static <T> Rest<T> success(T data,int count) {
        Rest<T> result = new Rest<>();
        result.setData(data);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMessage());
        result.setCount(count);
        result.setTimestamp();
        return result;
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     */
    public static <T> Rest<T> failed(IErrorCode errorCode) {

        Rest<T> result = new Rest<>();
        result.setCode(errorCode.getCode());
        result.setMsg(errorCode.getMessage());
        result.setTimestamp();
        return result;
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     * @param message   错误信息
     */
    public static <T> Rest<T> failed(IErrorCode errorCode, String message) {
        Rest<T> result = new Rest<>();
        result.setCode(errorCode.getCode());
        result.setMsg(message);
        result.setTimestamp();
        return result;
    }

    public static <T> Rest<T> failed(int code, String message) {
        Rest<T> result = new Rest<>();
        result.setCode(code);
        result.setMsg(message);
        result.setTimestamp();
        return result;
    }

    /**
     * 失败返回结果
     *
     * @param message 提示信息
     */
    public static <T> Rest<T> failed(String message) {

        Rest<T> result = new Rest<>();
        result.setCode(ResultCode.FAILED.getCode());
        result.setMsg(message);
        result.setTimestamp();
        return result;
    }

    /**
     * 失败返回结果
     */
    public static <T> Rest<T> failed() {
        return failed(ResultCode.FAILED);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> Rest<T> validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败返回结果
     *
     * @param message 提示信息
     */
    public static <T> Rest<T> validateFailed(String message) {
        Rest<T> result = new Rest<>();
        result.setCode(ResultCode.VALIDATE_FAILED.getCode());
        result.setMsg(message);
        result.setTimestamp();
        return result;
    }

    /**
     * 未登录返回结果
     */
    public static <T> Rest<T> unauthorized(T data) {

        Rest<T> result = new Rest<>();
        result.setCode(ResultCode.UNAUTHORIZED.getCode());
        result.setMsg(ResultCode.UNAUTHORIZED.getMessage());
        result.setData(data);
        result.setTimestamp();
        return result;
    }

    /**
     * 未授权返回结果
     */
    public static <T> Rest<T> forbidden(T data) {
        Rest<T> result = new Rest<>();
        result.setCode(ResultCode.FORBIDDEN.getCode());
        result.setMsg(ResultCode.FORBIDDEN.getMessage());
        result.setData(data);
        result.setTimestamp();
        return result;
    }

    public void setTimestamp() {
        this.timestamp = System.currentTimeMillis();
    }
}
